package com.huawei.hwid.f;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Bitmap.CompressFormat;
import android.graphics.BitmapFactory;
import android.graphics.BitmapFactory.Options;
import android.graphics.Point;
import android.net.Uri;
import android.os.Build.VERSION;
import android.os.Environment;
import android.text.TextUtils;
import android.util.DisplayMetrics;
import android.view.Display;
import android.view.WindowManager;
import com.huawei.hms.framework.network.restclient.hwhttp.route.IPRoute;
import com.huawei.hwid.update.provider.UpdateProvider;
import com.tencent.mm.opensdk.modelmsg.WXMediaMessage;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.lang.reflect.InvocationTargetException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

public class e {
    public static final String a = e.class.getSimpleName();

    private static String a() {
        Date date = new Date(System.currentTimeMillis());
        return "HwID_card_" + new SimpleDateFormat("yyyyMMdd_HHmmss", Locale.US).format(date) + ".jpg";
    }

    public static Uri a(Context context) {
        Object c = c(context);
        if (TextUtils.isEmpty(c)) {
            return null;
        }
        return Uri.fromFile(new File(c));
    }

    public static Uri a(Context context, String str) {
        if (TextUtils.isEmpty(str)) {
            return null;
        }
        return a(context, new File(str));
    }

    private static Uri a(Context context, File file) {
        if (context == null || file == null) {
            return null;
        }
        String str = context.getPackageName() + ".hwid.update.provider";
        if (b(context)) {
            return UpdateProvider.a(context, str, file);
        }
        return Uri.fromFile(file);
    }

    public static boolean b(Context context) {
        d dVar = new d(context);
        String packageName = context.getPackageName();
        String str = packageName + ".hwid.update.provider";
        if (VERSION.SDK_INT <= 23 || (context.getApplicationInfo().targetSdkVersion <= 23 && !dVar.a(packageName, str))) {
            return false;
        }
        return true;
    }

    public static String c(Context context) {
        Object d = d(context);
        if (TextUtils.isEmpty(d)) {
            return "";
        }
        return d + "/" + a();
    }

    public static String d(Context context) {
        String externalStorageState = Environment.getExternalStorageState();
        if (context == null) {
            return "";
        }
        if ("mounted_ro".equals(externalStorageState)) {
            return "";
        }
        if (!"mounted".equals(externalStorageState)) {
            return "";
        }
        File externalCacheDir = context.getExternalCacheDir();
        if (externalCacheDir != null) {
            try {
                return externalCacheDir.getCanonicalPath();
            } catch (IOException e) {
                com.huawei.hwid.core.d.b.e.b(a, "Exception", true);
                return "";
            }
        }
        externalStorageState = Environment.getExternalStorageDirectory().getPath() + "/" + context.getPackageName();
        File file = new File(externalStorageState);
        if (file.exists() || file.mkdir()) {
            return externalStorageState;
        }
        return "";
    }

    public static void e(Context context) {
        com.huawei.hwid.core.d.b.e.b(a, "deleteCachedFiles", true);
        Object d = d(context);
        if (!TextUtils.isEmpty(d)) {
            File file = new File(d);
            if (file.exists() && file.isDirectory()) {
                a(file);
            }
        }
    }

    private static void a(File file) {
        if (file != null) {
            File[] listFiles = file.listFiles();
            if (listFiles == null) {
                com.huawei.hwid.core.d.b.e.b(a, "deleteDirInnerPicFile files == null", true);
                return;
            }
            for (File file2 : listFiles) {
                if (file2.getName().startsWith("HwID_card_") && !file2.delete()) {
                    com.huawei.hwid.core.d.b.e.b(a, "delete file fail", true);
                }
            }
        }
    }

    public static Point f(Context context) {
        WindowManager windowManager = (WindowManager) context.getSystemService("window");
        if (windowManager == null) {
            return new Point();
        }
        Display defaultDisplay = windowManager.getDefaultDisplay();
        DisplayMetrics displayMetrics = new DisplayMetrics();
        defaultDisplay.getMetrics(displayMetrics);
        int i = displayMetrics.heightPixels;
        int i2 = displayMetrics.widthPixels;
        Point point = null;
        if (VERSION.SDK_INT >= 14 && VERSION.SDK_INT < 17) {
            point = a(defaultDisplay);
        } else if (VERSION.SDK_INT >= 17) {
            point = b(defaultDisplay);
        }
        if (point == null) {
            return new Point(i2, i);
        }
        return point;
    }

    private static Point a(Display display) {
        try {
            return new Point(((Integer) Display.class.getMethod("getRawWidth", new Class[0]).invoke(display, new Object[0])).intValue(), ((Integer) Display.class.getMethod("getRawHeight", new Class[0]).invoke(display, new Object[0])).intValue());
        } catch (IllegalArgumentException e) {
            com.huawei.hwid.core.d.b.e.b(a, "putil getRealScreenSize IllegalArgumentException ", true);
        } catch (IllegalAccessException e2) {
            com.huawei.hwid.core.d.b.e.b(a, "putil getRealScreenSize IllegalAccessException ", true);
        } catch (InvocationTargetException e3) {
            com.huawei.hwid.core.d.b.e.b(a, "putil getRealScreenSize InvocationTargetException ", true);
        } catch (NoSuchMethodException e4) {
            com.huawei.hwid.core.d.b.e.b(a, "putil getRealScreenSize NoSuchMethodException ", true);
        } catch (RuntimeException e5) {
            com.huawei.hwid.core.d.b.e.b(a, "putil getRealScreenSize RuntimeException ", true);
        } catch (Exception e6) {
            com.huawei.hwid.core.d.b.e.b(a, "putil getRealScreenSize Exception ", true);
        }
        return null;
    }

    private static Point b(Display display) {
        Point point = new Point();
        try {
            Display.class.getMethod("getRealSize", new Class[]{Point.class}).invoke(display, new Object[]{point});
            return new Point(point.x, point.y);
        } catch (IllegalArgumentException e) {
            com.huawei.hwid.core.d.b.e.b(a, "getRealScreenSize IllegalArgumentException ", true);
        } catch (IllegalAccessException e2) {
            com.huawei.hwid.core.d.b.e.b(a, "getRealScreenSize IllegalAccessException ", true);
        } catch (InvocationTargetException e3) {
            com.huawei.hwid.core.d.b.e.b(a, "getRealScreenSize InvocationTargetException ", true);
        } catch (NoSuchMethodException e4) {
            com.huawei.hwid.core.d.b.e.b(a, "getRealScreenSize NoSuchMethodException ", true);
        } catch (RuntimeException e5) {
            com.huawei.hwid.core.d.b.e.b(a, "getRealScreenSize RuntimeException ", true);
        } catch (Exception e6) {
            com.huawei.hwid.core.d.b.e.b(a, "getRealScreenSize Exception ", true);
        }
        return null;
    }

    public static boolean a(Context context, String str, int i, String str2) {
        Throwable th;
        Point f = f(context);
        Bitmap a = a(str, f.x > 0 ? f.x : 1920, f.y > 0 ? f.y : 1080);
        if (a == null) {
            com.huawei.hwid.core.d.b.e.b(a, "bitmap is null", true);
            return false;
        }
        OutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        int i2 = 100;
        a.compress(CompressFormat.JPEG, 100, byteArrayOutputStream);
        if (byteArrayOutputStream.toByteArray().length > 20971520) {
            return false;
        }
        while (byteArrayOutputStream.toByteArray().length > i * WXMediaMessage.DESCRIPTION_LENGTH_LIMIT) {
            int a2 = a(byteArrayOutputStream.toByteArray().length / WXMediaMessage.DESCRIPTION_LENGTH_LIMIT);
            byteArrayOutputStream.reset();
            i2 -= a2;
            a.compress(CompressFormat.JPEG, i2, byteArrayOutputStream);
        }
        FileOutputStream fileOutputStream = null;
        FileOutputStream fileOutputStream2;
        try {
            fileOutputStream2 = new FileOutputStream(new File(str2));
            try {
                fileOutputStream2.write(byteArrayOutputStream.toByteArray());
                fileOutputStream2.flush();
                fileOutputStream2.close();
                if (fileOutputStream2 != null) {
                    try {
                        fileOutputStream2.close();
                    } catch (IOException e) {
                        com.huawei.hwid.core.d.b.e.d(a, "fos.close IOException  ", true);
                    }
                }
            } catch (IOException e2) {
                try {
                    com.huawei.hwid.core.d.b.e.d(a, "IOException ", true);
                    if (fileOutputStream2 != null) {
                        try {
                            fileOutputStream2.close();
                        } catch (IOException e3) {
                            com.huawei.hwid.core.d.b.e.d(a, "fos.close IOException  ", true);
                        }
                    }
                    a.recycle();
                    return true;
                } catch (Throwable th2) {
                    Throwable th3 = th2;
                    fileOutputStream = fileOutputStream2;
                    th = th3;
                    if (fileOutputStream != null) {
                        try {
                            fileOutputStream.close();
                        } catch (IOException e4) {
                            com.huawei.hwid.core.d.b.e.d(a, "fos.close IOException  ", true);
                        }
                    }
                    throw th;
                }
            }
        } catch (IOException e5) {
            fileOutputStream2 = null;
            com.huawei.hwid.core.d.b.e.d(a, "IOException ", true);
            if (fileOutputStream2 != null) {
                fileOutputStream2.close();
            }
            a.recycle();
            return true;
        } catch (Throwable th4) {
            th = th4;
            if (fileOutputStream != null) {
                fileOutputStream.close();
            }
            throw th;
        }
        a.recycle();
        return true;
    }

    private static int a(int i) {
        if (i > 1000) {
            return 60;
        }
        if (i > 750) {
            return 40;
        }
        if (i > IPRoute.INTERVAL) {
            return 20;
        }
        return 10;
    }

    private static Bitmap a(String str, int i, int i2) {
        Options options = new Options();
        options.inJustDecodeBounds = true;
        BitmapFactory.decodeFile(str, options);
        int i3 = options.outWidth / i;
        int i4 = options.outHeight / i2;
        if (i3 >= i4) {
            i3 = i4;
        }
        if (i3 < 1) {
            i3 = 1;
        }
        com.huawei.hwid.core.d.b.e.b(a, "Image compression ratio", true);
        options.inSampleSize = i3;
        options.inJustDecodeBounds = false;
        return BitmapFactory.decodeFile(str, options);
    }
}
